#ifndef CAMERA_H
#define CAMERA_H

#include <QMainWindow>
#include <QImage>
#include <QTimer>
#include <QDir>
#include <QFileInfo>
#include <QDebug>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/videodev2.h>

namespace Ui {
class camera;
}

class camera : public QMainWindow
{
    Q_OBJECT

public:
    explicit camera(QWidget *parent = nullptr);
    ~camera();

private slots:
    void on_pushButton_clicked();    // 返回主界面
    void on_pushButton_2_clicked();  // 开启摄像头
    void on_pushButton_3_clicked();  // 关闭摄像头
    void on_pushButton_4_clicked();  // 抓拍
    void on_pushButton_5_clicked();  // 跳转抓拍相册界面
    void updateFrame();              // 更新摄像头画面

private:
    Ui::camera *ui;
    bool isCameraRunning = false;
    QTimer *timer;
    int captureWidth = 640;
    int captureHeight = 480;
    unsigned int pixelFormat = V4L2_PIX_FMT_YUYV;
    // 摄像头相关变量
    int fd = -1;
    unsigned char *mptr[4];
    unsigned int size[4];

    // 抓拍功能
    bool initCamera(); // 修改为返回bool
    void startCamera();
    void stopCamera();
    void destroyCamera();
    void getFrame(unsigned char *buffer);
    void yuyvToRgb(unsigned char *yuyvdata, unsigned char *rgbdata, int width, int height);
    void captureImage();
    QString captureDir = "./captured";
};

#endif // CAMERA_H
